﻿using System;
using System.Text;
using WebArchiveCopier;
using WebArchiveCopier.IO;

namespace WebArchiveCopier
{
	class Program
	{
		static void Main(string[] args)
		{
			try
			{
				if (args.Length != 2)
				{
					Usage();
				}
				else
				{
					var dataManager = new FileDataManager(args[1]);
					var itemListPage = new ItemListPage(args[0], dataManager);

					foreach (var item in itemListPage.Items)
					{
						Console.WriteLine(string.Format("Downloading {0}", item.Uri));
						var itemCalendar = new ItemCaptureCalendarPage(item.Uri, dataManager);

						foreach (var page in itemCalendar.Pages)
						{
							var captures = page.Captures;
							foreach (var date in captures)
							{
								foreach (var itemCaptureAddress in WebArchive.GetItemCaptureAddresses(item.Uri, date))
								{
									byte[] data = dataManager.GetData(itemCaptureAddress, item.Uri, date, true);
									if (data != null) break;
								}
							}
						}
					}
				}
			}
			catch (Exception e)
			{
				Console.Write(e.ToString());
			}
		}

		static void Usage()
		{
			Console.WriteLine("Bad number of arguments!");
			Console.WriteLine("Correct use:");
			Console.WriteLine("\t{0} domain output-directory", System.AppDomain.CurrentDomain.FriendlyName);
		}
	}
}
